<template>
  <DataList 
   ref="datalist"
  :excelHeader="excelHeader"
  :cate="cate" :cols="cols" 
  :name="name"  
  skipExcelNumber="3"

  @dlg-created="onDlgCreated"
  >
    <template slot="search">
     <el-form-item >
        <el-input v-model="search_str"  @change="clear" @input="mysearch" placeholder="请输入姓名或学号搜索" clearable></el-input>
      </el-form-item>
  </template>
  
  </DataList>
</template>
<script>
import DataList from "@/components/DataList";
import { getUser } from "@/utils/auth";
import {get在校生信息By班级,get在校生信息ByXSXH} from "@/api/在校生信息"
import {get班级} from "@/api/在校生信息"
import linq from "linq"
import {get开设专业} from '@/api/开设专业.js'
import { add在校生信息, delete在校生信息, update在校生信息, get在校生信息 ,bulkAddZxsxx } from "@/api/在校生信息.js";
import {findComponent} from "@/utils/ui";
import { Promise } from 'q';
export default {
  components: {
    DataList
  },
   mounted() {
    console.log(this.$root)
  },
  
  data() {
    return {
      search_str:'',
      cate: "A10-1-1学生基本信息",
      name:'10.1.1 学生基本信息',
      学生:[],
      excelHeader:{B3:"基本信息",P3:"行政班信息",B4:"学号",C4:"姓名",D4:"性别",E4:"民族",F4:"学籍状态",G4:"电子邮箱",H4:"生源地",I4:"来自军队",J4:"招生方式",K4:"生源类型",L4:"入学总分",M4:"是否常住户口在农村",N4:"是否订单（定向）培养",O4:"是否建档立卡贫困家庭",P4:"年级",Q4:"学制",R4:"入学日期（年月）",S4:"系部代码",T4:"所属系部",U4:"班级代码",V4:"班级名称（全称）",W4:"专业代码",X4:"专业名称（全称）",Y4:"专业方向代码",Z4:"专业方向名称（全称）",},
      cols: [
        { label: "基本信息", prop: "基本信息" ,
            data:[
            {label:'学号',prop:'XSXH'},
            {label:'姓名',prop:'XM'},
            {label:'性别',prop:'XZ_XB'},
            {label:'民族',prop:'XZ_MZ'},
            {label:'学籍状态',prop:'XZ_XJZT'},
            {label:'电子邮箱',prop:'DZYX'},
            {label:'生源地',prop:'SYD'},
            {label:'来自军队',prop:'XZ_SFJD'},
            {label:'招生方式',prop:'XZ_ZSFS'},
            {label:'生源类型',prop:'XZ_SYLX'},
            {label: "入学总分", prop: "RXCJZF" },
            {label:'是否常住户口在农村',prop:'XZ_SFNCHK'},
            {label:'是否订单(定向)培养',prop:'XZ_SFDDPY'},
            {label: "是否建档立卡贫困家庭", prop:"XZ_SFPKH" },
        ]},
        
        { label: "行政班信息",
          data: [
            { label: "年级", prop: "XZ_NJ" },
            { label: "学制", prop: "XZ_XZ" },
            { label: "入学日期(年月)", prop: "RXRQ" },
            { label: "系部代码", prop: "BMBH" },
            { label: "所属系部(全称)", prop: "BMMC" },
            { label: "班级代码", prop: "BJDM" },
            { label: "班级名称(全称)", prop: "BJMC" },
            { label: "专业代码", prop: "ZYDM" },
            { label: "专业名称(全称)", prop: "ZYMC" },
            { label: "专业方向代码", prop: "ZYFXDM" },
            { label: "专业方向名称(全称)", prop: "ZYFXMC" },
            
          ]
      }
       ]
  }
  },
  methods: {
   getSummaries(list){
      //
        const sums = [];
         this.cols.forEach((column, index) => {
            sums[index]="";
        });
        sums[0]="合计";
        sums[6]= "学生数:"+linq.from(list).distinct("$.XSXH").count();
        sums[8]= "女:"+linq.from(list).where("$.XZ_XB=='女'").count();
        sums[9]= "少数民族:"+linq.from(list).where("$.XZ_MZ!='汉族'").count();
        sums[10]= "辍学数:"+linq.from(list).where("$.XZ_XJZT=='辍学'").count();
        sums[13]= "军队:"+linq.from(list).where("$.XZ_SFJD!='否'").count();
        sums[17]= "农村:"+linq.from(list).where("$.XZ_SFNCHK=='是'").count();
        sums[18]= "订单:"+linq.from(list).where("$.XZ_SFDDPY=='是'").count();
        sums[19]= "贫困:"+linq.from(list).where("$.XZ_SFPKH=='是'").count();
        sums[24]= "系部数:"+linq.from(list).distinct("$.BMMC").count();
        sums[26]= "班级数:"+linq.from(list).distinct("$.BJMC").count();
        sums[28]= "专业数:"+linq.from(list).distinct("$.ZYMC").count();
        sums[30]= "方向数:"+linq.from(list).distinct("$.ZYFXMC").count();
        sums[31]= "通过:"+linq.from(list).where("$.审核状态=='通过'").count();
        sums[31] += ",不通过:"+linq.from(list).where("$.审核状态=='不通过'").count();
        sums[31] += ",待审核:"+linq.from(list).where("$.审核状态=='待审核'").count();
        
       return sums;
      //
    },
  async  onDlgCreated(model) {
            var dd = findComponent(this,"ZYFXMC");
          var ZYFXMC = linq.from( this.$root.开设专业).distinct("$.ZYFXMC").toArray();
        
          dd.$attrs.items = ZYFXMC.map(item => {
            return { label: item.ZYFXMC, value: item.ZYFXMC};
          });
    },
 
 
 

  async   onChange(model, field, value) {
      //
       if (field == "ZYFXMC") {
        
        var temp =  linq.from( this.$root.开设专业).first(item=>item.ZYFXMC==value);
        model.BMBH = temp.BMBH;
        model.BMMC = temp.BMMC;
        model.ZYMC = temp.ZYMC;
        model.ZYDM = temp.ZYDM;
        
        model.ZYFXDM = temp.ZYFXDM;
        
      }
      //
    },
    //导入检查方法
     check(array,list) {
     return new Promise(async (resolve,reject)=>{
         try{
          var user = getUser();

          var 专业 = await get开设专业(this.$root.user.XXID,this.$root.user.CJPC);
          //本身重复性检查
          
            var duplicates =  linq.from(array).groupBy("$.XSXH").where("$.count()>1").toArray();
            duplicates.forEach(e=>{
              linq.from(array).where(t=>t.XSXH==e.key()).forEach(t=>t.error="录入数据存在相同的学号");
            })
          
          //是否已经存在数据
        
          
            var err =  linq.from(array).select("$.XSXH").intersect(linq.from(list).select("$.XSXH").toArray()).toArray();
            linq.from(array).where(e=>err.indexOf(e.XSXH) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"学生表中已经存在对应的学号");
            
          //验证是否存在系部，专业，专业方向
          err =  linq.from(array).select("$.ZYDM+','+$.ZYFXDM").except(linq.from(专业).select("$.ZYDM+','+$.ZYFXDM").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.ZYDM+','+e.ZYFXDM) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"开设专业中不存在对应的专业，专业方向代码组合");
          
          linq
            .from(array)
            .forEach(e=>{
              var  kc = linq.from(专业).firstOrDefault(`$.ZYDM=='${e.ZYDM}' && $.ZYFXDM=='${e.ZYFXDM}'`,null);
              if(kc){
                e.ZYMC = kc.ZYMC;
              
                e.ZYFXMC = kc.ZYFXMC;
              }
            })
           // linq.from(array).where("!$.error")
            // linq.from(专业).where(e=>e.ZYFXDM == )
            resolve();
          }catch(e){
            reject(e);
          }
     }) 
    },
    
    async  bulkAdd(array){
       await  bulkAddZxsxx({Zxsxx:array});
    },
  
    async fetchData() {
      
      var user = getUser();
      var datas = await get在校生信息(user.XXID,user.CJPC);
      return datas;
    },
     onadd(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
      try{     
         
          console.log(JSON.stringify(temp));
          var model = await add在校生信息(temp);
          resolve(model);
      }catch(e){
        reject(e);
      }
      });
      
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
          console.log(JSON.stringify(temp));
          var model = await update在校生信息(temp);
          resolve(model);
      })
      //
    },
    ondelete(model) {
       var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
        
         await delete在校生信息(temp);
          resolve();
      })
    }
   
  }
};
</script>
<style>
</style>